pipeline {
    agent any
    stages {
<#if mode == "multistage">
    <#if sedScript != "">
        stage("sed") {
            steps {
                ${sedScript}
            }
        }
    </#if>
<#elseif mode == "jib">
        stage("build & jib") {
            steps {
                withCredentials([usernamePassword(credentialsId: "${repoCredential}", usernameVariable: "DOCKER_REPO_USERNAME", passwordVariable: "DOCKER_REPO_PASSWORD")]) {
                    sh 'echo "build" > Jenkins_result'
                    sh '''${tagCommand}'''
                    ${sedScript}
                    sh '''mvn \
                    clean compiler jib:build \
                    -Dmaven.test.skip=true \
                    -amd \
                    -pl ${moduleName} \
                    -Djib.to.auth.username=$DOCKER_REPO_USERNAME \
                    -Djib.to.auth.password=$DOCKER_REPO_PASSWORD \
                    -Djib.to.image=${dockerRepoAddress}/softwise/${imageName} \
                    -Djib.to.tags=${tags}
                    '''
                    sh 'echo "" > Jenkins_result'
                }
            }
        }
<#else>
        stage("build") {
            steps {
                sh 'echo "build" > Jenkins_result'
                ${sedScript}
                sh '''mvn \
                clean install \
                -Dmaven.test.skip=true \
                -amd \
                -pl ${moduleName}
                '''
                sh 'echo "" > Jenkins_result'
            }
        }
</#if>
<#--docker 阶段-->
<#if mode == "multistage">
        stage("docker") {
            steps {
                withCredentials([usernamePassword(credentialsId: "${repoCredential}", usernameVariable: "DOCKER_REPO_USERNAME", passwordVariable: "DOCKER_REPO_PASSWORD")]) {
                    sh 'echo "build" > Jenkins_result'
                    sh '''${tagCommand}'''
                    sh 'docker login ${dockerRepoAddress} -u ${r'$DOCKER_REPO_USERNAME'} -p ${r'$DOCKER_REPO_PASSWORD'}'
                    sh '''
                    image_tag=""
                    for tag in `cat .tags`; do
                        image_tag=${r'${image_tag}'}"-t ${imageName}:${r'${tag}'} "
                    done
                    eval "docker build ${r'${image_tag}'} ."
                    '''
                    sh '''
                    for tag in `cat .tags`; do
                        docker push ${imageName}:${r'${tag}'}
                    done
                    '''
                    sh 'docker logout ${dockerRepoAddress}'
                    sh 'echo "" > Jenkins_result'
                }
            }
        }
<#elseif mode == "jib">

<#else>
        stage("docker") {
            steps {
                withCredentials([usernamePassword(credentialsId: "${repoCredential}", usernameVariable: "DOCKER_REPO_USERNAME", passwordVariable: "DOCKER_REPO_PASSWORD")]) {
                    sh 'echo "build" > Jenkins_result'
                    sh '''${tagCommand}'''
                    sh 'docker login ${dockerRepoAddress} -u ${r'$DOCKER_REPO_USERNAME'} -p ${r'$DOCKER_REPO_PASSWORD'}'
                    sh '''
                    image_tag=""
                    for tag in `cat .tags`; do
                        image_tag=${r'${image_tag}'}"-t ${imageName}:${r'${tag}'} "
                    done
                    eval "docker build ${r'${image_tag}'} ./${moduleName}"
                    '''
                    sh '''
                    for tag in `cat .tags`; do
                        docker push ${imageName}:${r'${tag}'}
                    done
                    '''
                    sh 'docker logout ${dockerRepoAddress}'
                    sh 'echo "" > Jenkins_result'
                }
            }
        }
</#if>
        stage("deploy") {
            steps {
                sh 'echo "deploy" > Jenkins_result'
                sh '''k8s-cmd deploy \
                --kubeconfig ${kubeconfig} \
                -n ${namespace} \
                -a ${deploymentName} \
                -i ${allImageName} \
                -T `cat .tag`<#if k8sYamlDir?exists> --k8s-yaml-dir ${k8sYamlDir}</#if>
                '''
                sh 'echo "" > Jenkins_result'
            }
        }
        stage("check") {
            steps {
                sh 'echo "check" > Jenkins_result'
                sh '''k8s-cmd check \
                --kubeconfig ${kubeconfig} \
                -n ${namespace} \
                -a ${deploymentName} \
                -i ${allImageName} \
                -T `cat .tag`
                '''
                sh 'echo "" > Jenkins_result'
            }
        }
    }
    post {
        success {
            // Usage remind-v2.py {cluster} 1 {msg}
            sh 'remind-v2.py ${cluster} 1 ${msg}'
        }
        failure {
            // Usage remind-v2.py {cluster} 0 {msg}
            sh 'remind-v2.py ${cluster} 0 ${msg}'
        }
    }
}
